﻿using Autofac;
using Common;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;

namespace ZoneLine.ViewModels
{
    public class PieceIndexViewModel : ViewModelBase
    {
        private IModelDAL _dal;

        public PieceIndexViewModel(IModelDAL dal)
        {
            _dal = dal;
        }

        private List<PieceIndexModel> _records;

        public List<PieceIndexModel> Records
        {
            get { return _records; }
            set => SetProperty(ref _records, value);
        }

        private RelayCommand _freshCmd;

        public RelayCommand FreshCmd
        {
            get { return _freshCmd ?? (_freshCmd = new RelayCommand(FreshRecords)); }
        }

        private void FreshRecords()
        {
            try
            {
                LoadingModel.IsBusy = true;
                Records = null;
                var dt = DateTime.Now;
                var days = (dt - DateTime.Parse("2020-01-01")).Days;
                Records = _dal.GetModelList<PieceIndexModel>(p => p.DayIndex == days);
                LoadingModel.ActionSuccess = true;
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex, "search pieceindexmodel err");
                LoadingModel.ActionFailed = true;
            }
        }
    }
}
